#include "Queue.h"

void DuiLieInit(pp* phead)
{
	assert(phead);
	phead->size = 0;
	phead->head = phead->ptail=NULL;
}

void DLpushBack(pp* phead, SLTDdatatype x)
{
	assert(phead);
	Queue* tmp = (Queue*)malloc(sizeof(Queue));
	if (tmp == NULL)
	{
		perror("tmp fail !");
		exit(1);
	}
	tmp->data = x;
	tmp->next = NULL;
	if (phead->ptail == NULL)
	{
		phead->head = phead->ptail = tmp;
	}
	else
	{
		phead->ptail->next = tmp;
		phead->ptail = tmp;
	}
	phead->size++;
}

void my_printf(pp* phead)
{
	assert(phead);
	Queue* ps = phead->head;
	while (ps)
	{
		printf("%d ->", ps->data);
		ps = ps->next;
	}
	printf("\n");
}

void DLpopFront(pp* phead)
{
	assert(phead&&phead->head);
	pp* tmp = phead->head;
	phead->head = phead->head->next;
	free(tmp);
	tmp = NULL;
	phead->size--;
}

SLTDdatatype GetDdata(pp* phead)
{
	assert(phead&&phead->head);
	return phead->head->data;
}

SLTDdatatype GetBackdata(pp* phead)
{
	assert(phead&&phead->ptail);
	return phead->ptail->data;
}

bool DLempty(pp*phead)
{
	assert(phead);
	if (phead->ptail == NULL)
		return false;
	else
		return true;
}

int getshuju(pp* phead)
{
	assert(phead);
	return phead->size;
}

void Destroy(pp* phead)
{
	assert(phead);
	Queue* tmp=NULL;
	while (phead->head)
	{
		 tmp = phead->head;
		free(tmp);
		phead->head = phead->head->next;
	}
	tmp = NULL;
	phead->head = phead->ptail = NULL;
	phead->size = 0;
}